package haizhi.fruitmall.service.impl;

import com.github.pagehelper.PageHelper;
import haizhi.fruitmall.dao.mapper.GoodsCategoryMapper;
import haizhi.fruitmall.exception.ResultException;
import haizhi.fruitmall.pojo.form.GoodsCategoryForm;
import haizhi.fruitmall.pojo.po.GoodsCategory;
import haizhi.fruitmall.pojo.po.GoodsCategoryExample;
import haizhi.fruitmall.pojo.view.GoodsCategoryView;
import haizhi.fruitmall.service.GoodsCategoryService;
import haizhi.fruitmall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by richa on 2017/9/13
 */

@Service
@Transactional
public class GoodsCategoryServiceImpl implements GoodsCategoryService{

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public void insert(GoodsCategoryForm form) {
        if(BeanUtil.isEmpty(form.getCategoryName())){
            throw new ResultException("分类名称不能为空！");
        }else{
            GoodsCategoryExample example = new GoodsCategoryExample();
            example.or().andCategoryNameEqualTo(form.getCategoryName());
            if(BeanUtil.noEmpty(goodsCategoryMapper.selectByExample(example))){
                throw new ResultException("分类名称已经存在！");
            }
        }
        if(BeanUtil.noNull(form.getCategoryPid())){
            GoodsCategory goodsCategory = goodsCategoryMapper.selectByPrimaryKey(form.getCategoryPid());
            if(BeanUtil.isNull(goodsCategory) || BeanUtil.noNull(goodsCategory.getCategoryPid())){
                throw new ResultException("父级分类不存在！");
            }
        }
        GoodsCategory record = new GoodsCategory();
        BeanUtil.copyBean(form,record);
        record.setCategoryId(BeanUtil.intId());
        goodsCategoryMapper.insertSelective(record);
    }

    @Override
    public void update(GoodsCategoryForm form){
        if(BeanUtil.noNull(form.getCategoryPid())){
            GoodsCategory goodsCategory = goodsCategoryMapper.selectByPrimaryKey(form.getCategoryPid());
            if(BeanUtil.isNull(goodsCategory) || BeanUtil.noNull(goodsCategory.getCategoryPid())){
                throw new ResultException("父级分类不存在！");
            }
        }
        GoodsCategory record = goodsCategoryMapper.selectByPrimaryKey(form.getCategoryId());
        if(BeanUtil.isNull(record)){ throw new ResultException("记录不存在！"); }
        BeanUtil.copyBean(form,record);
        record.setUpdateTime(BeanUtil.newTime());
        goodsCategoryMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void delete(Integer id) {
        if(BeanUtil.isNull(id)){ throw new ResultException("编号为空！");}
        if(BeanUtil.isNull(goodsCategoryMapper.selectByPrimaryKey(id))){ throw new ResultException("记录不存在！"); }
        goodsCategoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public GoodsCategory getone(Integer id) {
        if(BeanUtil.isNull(id)){ throw new ResultException("编号为空！");}
        return goodsCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<GoodsCategory> getall(GoodsCategoryForm form) {
        GoodsCategoryExample example = new GoodsCategoryExample();
        GoodsCategoryExample.Criteria criteria = example.createCriteria();
        if(BeanUtil.noNull(form.getCategoryId())){
            criteria.andCategoryIdEqualTo(form.getCategoryId());
        }
        if(BeanUtil.noEmpty(form.getCategoryName())) {
            criteria.andCategoryNameLike(BeanUtil.isLike(form.getCategoryName()));
        }
        if(BeanUtil.noNull(form.getIsParent())){
            criteria.andCategoryPidIsNull();
        }
        if (BeanUtil.noEmpty(form.getCategoryIdList())) {
            criteria.andCategoryIdIn(form.getCategoryIdList());
        }
        if(BeanUtil.noEmpty(form.getCategoryPidList())){
            criteria.andCategoryPidIn(form.getCategoryPidList());
        }
        if(BeanUtil.noNull(form.getPageNum()) && BeanUtil.noNull(form.getPageSize())){
            PageHelper.startPage(form.getPageNum(),form.getPageSize());
        }
        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectByExample(example);
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
        for(GoodsCategory goodsCategory : goodsCategoryList){
           if(BeanUtil.noEmpty(goodsCategory.getPicturePath())){
               String picturePath = BeanUtil.getPath(request)+goodsCategory.getPicturePath();
               goodsCategory.setPicturePath(picturePath);
           }
        }
        return goodsCategoryList;
    }

    @Override
    public List<GoodsCategoryView> getView(GoodsCategoryForm form){
        List<GoodsCategory> parentList = this.getall(form);
        List<GoodsCategoryView> parentViewList = new ArrayList<>();
        if(BeanUtil.noEmpty(parentList)){
            List<Integer> parentIdList = new ArrayList<>();
            for(GoodsCategory parent : parentList){
                GoodsCategoryView parentView = new GoodsCategoryView();
                BeanUtil.copyBean(parent,parentView);
                parentViewList.add(parentView);
                parentIdList.add(parent.getCategoryId());
            }
            List<GoodsCategory> childList = this.getall(new GoodsCategoryForm(parentIdList));
            if(BeanUtil.noEmpty(childList)){
                for(GoodsCategory child : childList){
                    for(GoodsCategoryView parentView : parentViewList){
                        if(child.getCategoryPid().equals(parentView.getCategoryId())){
                            GoodsCategoryView childView = new GoodsCategoryView();
                            BeanUtil.copyBean(child,childView);
                            parentView.addChild(childView);
                        }
                    }
                }
            }
        }
        return parentViewList;
    }

}
